package com.imooc.miaosha.task;

import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.service.OrderService;
import com.imooc.miaosha.util.JedisUtil;
import com.imooc.miaosha.util.RedissonManage;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.TimeUnit;
@Slf4j
@Controller
public class CloseOrderTask {
    @Autowired
    RedissonManage redissonManage;
    @Autowired
    OrderService orderService;
  @Scheduled(cron = "0 0/15 * * * ? ")

    public void closeOrder(){
        RLock rLock=redissonManage.getRedisson().getLock(Const.RLOCK_NAME.CLOSE_ORDER_LOCK);
        try {
            if (rLock.tryLock(0,2,TimeUnit.SECONDS)){
                log.info("========抢锁成功============");
                orderService.closeOrder();
                rLock.unlock();
            }
            else {
                log.info("========抢锁失败============");
                }

        } catch (InterruptedException e) {
            rLock.unlock();
            e.printStackTrace();
        }
    }
}
